package com.itwelkin.ssm.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwelkin.ssm.model.Customer;
import com.itwelkin.ssm.model.Page;
import com.itwelkin.ssm.service.CustomerService;
import com.itwelkin.ssm.utils.SystemUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("customer")
public class CustomerController {
    private CustomerService customerService;

    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @RequestMapping("customerlist")
    public void customerlist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(SystemUtils.checkSession(request,response,false)){return;}
        //查询所有客户信息
        List<Customer> customers = customerService.queryCustomerList();
        //将获取的客户信息响应到前台
        request.setAttribute("customers",customers);
        request.getRequestDispatcher("list").forward(request,response);
    }
    @RequestMapping("listWithCondition")
    public void listWithCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        if(SystemUtils.checkSession(request,response,false)){return;}
        //接收参数
        String cname = request.getParameter("cname");
        String startAge = request.getParameter("startAge");
        String endAge = request.getParameter("endAge");
        String clevel = request.getParameter("clevel");
        //数据回显
        request.setAttribute("cname",cname);
        request.setAttribute("startAge",startAge);
        request.setAttribute("endAge",endAge);
        request.setAttribute("clevel",clevel);
        //正式处理用户请求
        List<Customer> customers = customerService.queryCustomerListWithCondtion(cname,startAge,endAge,clevel);
        request.setAttribute("customers",customers);
        request.getRequestDispatcher("customerList.jsp").forward(request,response);
    }
    @RequestMapping("add")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        Map<String,String[]> parameterMap = request.getParameterMap();
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class aClass, Object o) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = null;
                try {
                    String[] split = request.getParameter("birthday").split("T");
                    parse = simpleDateFormat.parse(split[0]);
                }catch (ParseException e){
                    e.printStackTrace();
                }
                return parse;
            }
        },Date.class);
        Customer customer = new Customer();
        try {
            BeanUtils.populate(customer,parameterMap);
        }catch (Exception e){
            e.printStackTrace();
        }
        customer.setRegTime(new Date());
        customer.setId(SystemUtils.getUUID());
        if (SystemUtils.checkVerify(request,"customer",customer)==null)return "http://localhost:8080/ssm_biao7/customer/tian.jsp";

        //去服务层正式添加
        customerService.add(customer);
        return "http://localhost:8080/ssm_biao7/customer/customerlist";
    }

    @RequestMapping("delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //检查
        if(SystemUtils.checkSession(request,response,false)){return;}
        //获取id
        String id = request.getParameter("id");
        customerService.deleteCustomer(id);
        request.setAttribute("msg","删除成功");
        request.getRequestDispatcher("customerlist").forward(request,response);
    }

    @RequestMapping("deleteWithMore")
    public void deleteWithMore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //检查
        if(SystemUtils.checkSession(request,response,false)){return;}
        //获取id
        String[] ids = request.getParameterValues("ids");
        customerService.deleteCustomerWithMore(ids);
        request.setAttribute("msg","删除成功");
        request.getRequestDispatcher("customerlist").forward(request,response);
    }

    @RequestMapping("update")
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(SystemUtils.checkSession(request,response,false)){return;}

        String id = request.getParameter("id");
        Customer customer = customerService.queryCustomerById(id);
        request.setAttribute("customer",customer);
        request.getRequestDispatcher("/customer/xiu.jsp").forward(request,response);
    }

    @RequestMapping("trueUpdate")
    public String trueUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Customer customer = new Customer();
        if(SystemUtils.checkSession(request,response,false)){return "http://localhost:8080/ssm_biao7/customer/customerList.jsp";}


        Map<String,String[]> parameterMap = request.getParameterMap();

        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class aClass, Object o) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = null;
                try {
                    String[] split = request.getParameter("birthday").split("T");
                    parse = simpleDateFormat.parse(split[0]);
                }catch (ParseException e){
                    e.printStackTrace();
                }
                return parse;
            }
        },Date.class);

        try {
            BeanUtils.populate(customer,parameterMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (SystemUtils.checkVerify(request,"customer",customer)==null){
            return "http://localhost:8080/ssm_biao7/customer/update?id="+customer.getId();
        }
        customerService.updateCustomerById(customer);

        return "http://localhost:8080/ssm_biao7/customer/customerlist";
    }
    @RequestMapping("list")
    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPage = request.getParameter("currentPage");
        if (currentPage == null){
            PageHelper.startPage(1,15);
        }else {
            PageHelper.startPage(Integer.parseInt(currentPage),15);
        }
        List<Customer> customerList = customerService.queryCustomerList();
        PageInfo<Customer> customerPageInfo = new PageInfo<>(customerList);
        List<Customer> customers = customerPageInfo.getList();
        long total = customerPageInfo.getTotal(); //总记录数
        int pageNum = customerPageInfo.getPageNum();//当前页码
        int pages = customerPageInfo.getPages();//总页数
        Page page = new Page();
        page.setCount(total);
        page.setCurrentPage(pageNum);
        page.setPageCount(pages);
        if (pageNum<=5){
            page.setStartPage(1);
            page.setEndPage(10);
        }else{
            page.setStartPage(pageNum-4);
            if(pageNum+4>12){
                page.setStartPage(pageNum-4);
                page.setEndPage(13);
            }else{
                page.setEndPage(pageNum+4);
            }
        }

        request.setAttribute("page",page);
        request.setAttribute("customers",customers);
        request.getRequestDispatcher("/customer/customerList.jsp").forward(request,response);


    }

}
